/*
 * Copyright (c) Huawei Technologies Co.,Ltd. 2023. All rights reserved.
 */
package org.opengauss.utils;

import java.util.Locale;
import java.util.Random;

/**
 * Title: the StringTool class.
 * <p>
 * Description:
 *
 * @author justbk
 * @version [Tools 0.0.1, 2023/8/8]
 * @since 2023/8/8
 */
public class StringTool {
    public static final String RANDOM_CHAR_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String RANDOM_CHAR_ALL = RANDOM_CHAR_UPPER
            + RANDOM_CHAR_UPPER.toLowerCase(Locale.ROOT)
            + "0123456789";
    private static long randomKey = System.currentTimeMillis();
    private static Random curRandom = new Random(randomKey);
    
    public static String randomString(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            Character chr = RANDOM_CHAR_ALL.charAt(curRandom.nextInt(RANDOM_CHAR_ALL.length()));
            sb.append(chr);
        }
        return sb.toString();
    }
    
    // begin must more than 0
    public static String randString(int begin, int end) {
        int curLength = curRandom.nextInt(end - begin) + begin;
        return randomString(curLength);
    }
}
